<?php

class Courses extends Controller implements IExceptionHandler {

    public function ExceptionHandler(Exception $e) {
        $this->error = $e;
        if ($e instanceof AuthenticationException) {
            exit;
        } else {
            $this->view('main/index');
        }
    }

    private function Authenticate() {
        if (!in_array($this->user->get_id_profile_type(), [ PROFILE_SUPER_ADMINISTRATEUR, PROFILE_ADMINISTRATEUR])) {
            global $MESSAGE_0001;
            throw new AuthenticationException($MESSAGE_0001);
        }
    }

    public function GET_list() {
        $this->Authenticate();
        $this->view('main/courses/list');
    }

    public function GET_create() {
        $this->Authenticate();
        $this->view('main/profiles/create');
    }

    public function POST_create($name, $type) {
        $this->Authenticate();

        $this->user->create_profile([
            ':designation' => $name,
            ':id_profil_type' => $type,
        ]);

        $this->view('main/index');
    }

    public function GET_update() {
        $this->Authenticate();
        $this->view('main/profiles/update');
    }

    public function POST_update($id, $name, $type) {
        $this->Authenticate();

        $this->user->update_profile([
            ':id_profil' => $id,
            ':designation' => $name,
            ':id_profil_type' => $type,
        ]);

        $this->view('main/index');
    }

    public function POST_delete($id) {
        $this->Authenticate();

        $this->user->delete_profile([':id_profil' => $id]);

        $this->view('main/index');
    }

}
